package com.transcargo.users;

import java.util.ArrayList;
import java.util.Iterator;

import com.transcargo.data.Cache;
import com.transcargo.db.db;
import com.transcargo.db.dbException;
import com.transcargo.geo.GeoObjectNotFoundException;


public class ShortUser {
	private int Id = -1;
    private String name = "";
    private String address = "";
    private String city = "";
    private String country = null;
    private String phone = "";
    private String email = "";
    private String url = "";
    private String skype = "";
    private String icq = "";
    private String info = "";
    private String timezone = "";
    private String registered = "";
    private int num_feeds_good = 0;
    private int num_feeds_neutral = 0;
    private int num_feeds_bad = 0;
    
    public ShortUser(int id, db dbase) throws UserNotFoundException, GeoObjectNotFoundException {
		Id = id;
        try {
            ArrayList<String[]> list = dbase.execute("SELECT organization, address, city, country_id, phone, email, web_url, skype, icq, timezone, info, registered FROM registered_users WHERE id=" + Integer.toString(id));
            if (list != null) {
                Iterator<String[]> iter =list.iterator();
                if (iter.hasNext()) {
                    String[] ctr = iter.next();
                    name = ctr[0];
                    address = ctr[1];
                    city = ctr[2];
                    phone = ctr[4];
                    email = ctr[5];
                    url = ctr[6];
                    skype = ctr[7];
                    icq = ctr[8];
                    info = ctr[10];
                    timezone = ctr[9];
                    registered = ctr[11];
                    country = Cache.getCountry(Integer.parseInt(ctr[3])).getName();
                    
                	list = dbase.execute("SELECT count(id) FROM feedbacks WHERE rating<0 AND user_id=" + Id);
                	iter = list.iterator();
                	if (iter.hasNext()) {
                		ctr = iter.next();
                		num_feeds_bad = Integer.parseInt(ctr[0]);
                	}
                	list = dbase.execute("SELECT count(id) FROM feedbacks WHERE rating=0 AND user_id=" + Id);
                	iter = list.iterator();
                	if (iter.hasNext()) {
                		ctr = iter.next();
                		num_feeds_neutral = Integer.parseInt(ctr[0]);
                	}
                	list = dbase.execute("SELECT count(id) FROM feedbacks WHERE rating>0 AND user_id=" + Id);
                	iter = list.iterator();
                	if (iter.hasNext()) {
                		ctr = iter.next();
                		num_feeds_good = Integer.parseInt(ctr[0]);
                	}
                } else throw new UserNotFoundException(id);
            } else throw new UserNotFoundException(id);
        } catch (dbException e) {
            throw new UserNotFoundException(id);
        }
	}
	public String getName() {
		return name;
	}
    public String getAddress() {
        return address;
    }
    public String getCity() {
        return city;
    }
    public String getPhone() {
        return phone;
    }
    public String getEmail() {
        return email;
    }
    public String getUrl() {
        return url;
    }
    public String getSkype() {
        return skype;
    }
    public String getIcq() {
        return icq;
    }
    public String getInfo() {
        return info;
    }
    public String getTimezone() {
        return timezone;
    }
    public String getCountry() {
        return country;
    }
    public String getRegisterDate() {
        return registered;
    }

    public int getNumGoodFeedbacks() {
    	return num_feeds_good;
    }
    public int getNumNeutralFeedbacks() {
    	return num_feeds_neutral;
    }
    public int getNumBadFeedbacks() {
    	return num_feeds_bad;
    }
}
